﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_BankAccount : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string account_id;

        public frm_Edit_BankAccount()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.account_id = null;
            LoadCurrency();            
        }

        //////////////////////////////////////////////
        /// Load data
        //////////////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(account_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT BankAccountNo, BankName, CurrencyID"
                        + " FROM [222_BankAccount] WHERE BankAccountID='" + account_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.accountID.Text = account_id;
                        this.accountNo.Text = dataReader["BankAccountNo"].ToString();
                        this.bankName.Text = dataReader["BankName"].ToString();
                        for (int i = 0; i < this.currencyID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.currencyID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CurrencyID"].ToString())
                            {
                                this.currencyID.SelectedIndex = i;
                            }
                        }
                    }
                    else
                    {
                        ret = false;
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }

            return ret;
        }

        ////////////////////////////////////////////
        /// Load Currency List
        ////////////////////////////////////////////
        private void LoadCurrency()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CurrencyID FROM [222_Currency] ORDER BY CurrencyID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CurrencyID"].ToString();
                    item.Value = dataReader["CurrencyID"].ToString();
                    this.currencyID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Currency list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string account_number = null;
            string bank_name = null;            
            string currency_id = null;
            
            ComboboxItem item = new ComboboxItem();            

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.accountNo.Text))
                {
                    error = "Please enter Bank Account No.";
                }
                else
                {
                    account_number = this.accountNo.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.bankName.Text))
                {
                    error = "Please enter Bank Name";
                }
                else
                {
                    bank_name = this.bankName.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.currencyID.SelectedIndex < 0)
                {
                    error = "Please select Currency";
                }
                else
                {
                    item = (ComboboxItem)this.currencyID.SelectedItem;
                    currency_id = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_BankAccount] SET BankAccountNo='" + account_number + "',"
                        + " BankName='" + bank_name + "', CurrencyID='" + currency_id + "'"
                        + " WHERE BankAccountID='" + account_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Bank Account updated success.");
                    this.Dispose();                    
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Bank Account. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
